package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipReqularCheckItem;
import cc.comac.mom.bean.EquipReqularCheckItemBean;
import cc.comac.mom.dao.entity.EquipReqularCheckItemDao;
import cc.comac.mom.entity.EquipReqularCheckItemEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipReqularCheckItemDataHandler extends ServiceImpl<EquipReqularCheckItemDao, EquipReqularCheckItemEntity> {
	@Autowired
	private EquipReqularCheckItemDao EquipReqularCheckItemDao;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	public ResultBean save(EquipReqularCheckItem obj) {
		ResultBean resultBean = new ResultBean(true);
		//校验数据
		ResultBean resultBean2 = checkData(obj);
		if(!resultBean2.success) {
			return resultBean2;
		}
		EquipReqularCheckItemEntity innerData = obj.getInnerData();
		baseEntityDataUtil.setBaseEntityData(innerData);
		int insert = EquipReqularCheckItemDao.insert(innerData);
		if(insert<1) {
			throw new RuntimeException("新增失败");
		}
		return resultBean;
	}
	
	public ResultBean saveBatch(List<EquipReqularCheckItem> list) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(list == null || list.size()<1) {
			throw new RuntimeException("数据为空");
		}
		ArrayList<EquipReqularCheckItemEntity> arrayList = new ArrayList<EquipReqularCheckItemEntity>();
		for(EquipReqularCheckItem item :list) {
			//校验数据
			ResultBean resultBean2 = checkData(item);
			if(!resultBean2.success) {
				return resultBean2;
			}
			EquipReqularCheckItemEntity innerData = item.getInnerData();
			baseEntityDataUtil.setBaseEntityData(innerData);
			arrayList.add(innerData);
		}
		
    	QueryWrapper<EquipReqularCheckItemEntity> queryWrapper = new QueryWrapper<EquipReqularCheckItemEntity>();
    	queryWrapper.eq("row_status",1);
    	queryWrapper.eq("regular_check_id",list.get(0).getInnerData().getRegularCheckId());
    	queryWrapper.orderByDesc("benchmark_seq");
    	Page<EquipReqularCheckItemEntity> page = new Page<>(0,1, false);
    	
    	IPage<EquipReqularCheckItemEntity> iPage = EquipReqularCheckItemDao.selectPage(page, queryWrapper);
    	List<EquipReqularCheckItemEntity> equipReqularCheckItemEntityList = iPage.getRecords();
    	int i=0;
    	if(equipReqularCheckItemEntityList != null && equipReqularCheckItemEntityList.size()>0) {
    		i=equipReqularCheckItemEntityList.get(0).getBenchmarkSeq()+1;
    	}
    	for(int b= i;b<arrayList.size()+i;b++) {
    		arrayList.get(b-i).setBenchmarkSeq(b);
    	}
		boolean saveBatch = saveBatch(arrayList);
		if(!saveBatch) {
			throw new RuntimeException("新增失败");
		}
		return resultBean;
	}
	
	public ResultBean update(EquipReqularCheckItem obj) {
		ResultBean resultBean = new ResultBean(true);
		//校验数据
		ResultBean resultBean2 = checkData(obj);
		if(!resultBean2.success) {
			return resultBean2;
		}
		EquipReqularCheckItemEntity innerData = obj.getInnerData();
		baseEntityDataUtil.updateBaseEntityData(innerData);
		int update = EquipReqularCheckItemDao.updateById(innerData);
		if(update<1) {
			throw new RuntimeException("修改失败");
		}
		return resultBean;
	}
	
	public ResultBean moveUp(EquipReqularCheckItem obj) {
		ResultBean resultBean = new ResultBean(true);
		//校验数据
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipReqularCheckItemEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");
		}
		EquipReqularCheckItemEntity selectById = EquipReqularCheckItemDao.selectById(innerData.getId());
		if(selectById == null) {
			throw new RuntimeException("上移数据为空");
		}
    	QueryWrapper<EquipReqularCheckItemEntity> queryWrapper = new QueryWrapper<EquipReqularCheckItemEntity>();
    	queryWrapper.eq("row_status",1);
    	queryWrapper.eq("regular_check_id",selectById.getRegularCheckId());
    	queryWrapper.lt("benchmark_seq", selectById.getBenchmarkSeq());
    	queryWrapper.orderByDesc("benchmark_seq");
    	Page<EquipReqularCheckItemEntity> page = new Page<>(0,1, false);
    	IPage<EquipReqularCheckItemEntity> iPage = EquipReqularCheckItemDao.selectPage(page, queryWrapper);
    	List<EquipReqularCheckItemEntity> list = iPage.getRecords();
    	if(list == null || list.size()<1) {
			throw new RuntimeException("已经是第一条数据，无法上移");
    	}
    	EquipReqularCheckItemEntity EquipReqularCheckItemEntity = list.get(0);
    	
    	int temp = selectById.getBenchmarkSeq();
    	selectById.setBenchmarkSeq(EquipReqularCheckItemEntity.getBenchmarkSeq());
    	EquipReqularCheckItemEntity.setBenchmarkSeq(temp);
    	baseEntityDataUtil.updateBaseEntityData(selectById);
    	baseEntityDataUtil.updateBaseEntityData(EquipReqularCheckItemEntity);
    	
    	ArrayList<EquipReqularCheckItemEntity> arrayList = new ArrayList<EquipReqularCheckItemEntity>();
    	arrayList.add(EquipReqularCheckItemEntity);
    	arrayList.add(selectById);
    	boolean batchById = updateBatchById(arrayList);

		if(!batchById) {
			throw new RuntimeException("上移失败");
		}
		return resultBean;
	}
	
	public ResultBean moveDown(EquipReqularCheckItem obj) {
		ResultBean resultBean = new ResultBean(true);
		//校验数据
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipReqularCheckItemEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");
		}
		EquipReqularCheckItemEntity selectById = EquipReqularCheckItemDao.selectById(innerData.getId());
		if(selectById == null) {
			throw new RuntimeException("上移数据为空");
		}
    	QueryWrapper<EquipReqularCheckItemEntity> queryWrapper = new QueryWrapper<EquipReqularCheckItemEntity>();
    	queryWrapper.eq("row_status",1);
    	queryWrapper.eq("regular_check_id",selectById.getRegularCheckId());
    	queryWrapper.gt("benchmark_seq", selectById.getBenchmarkSeq());
    	queryWrapper.orderByAsc("benchmark_seq");
    	Page<EquipReqularCheckItemEntity> page = new Page<>(0,1, false);
    	IPage<EquipReqularCheckItemEntity> iPage = EquipReqularCheckItemDao.selectPage(page, queryWrapper);
    	List<EquipReqularCheckItemEntity> list = iPage.getRecords();
    	if(list == null || list.size()<1) {
			throw new RuntimeException("已经是最后一条数据，无法下移");
    	}
    	EquipReqularCheckItemEntity EquipReqularCheckItemEntity = list.get(0);
    	
    	int temp = selectById.getBenchmarkSeq();
    	selectById.setBenchmarkSeq(EquipReqularCheckItemEntity.getBenchmarkSeq());
    	EquipReqularCheckItemEntity.setBenchmarkSeq(temp);
    	baseEntityDataUtil.updateBaseEntityData(selectById);
    	baseEntityDataUtil.updateBaseEntityData(EquipReqularCheckItemEntity);
    	
    	ArrayList<EquipReqularCheckItemEntity> arrayList = new ArrayList<EquipReqularCheckItemEntity>();
    	arrayList.add(EquipReqularCheckItemEntity);
    	arrayList.add(selectById);
    	boolean batchById = updateBatchById(arrayList);

		if(!batchById) {
			throw new RuntimeException("下移失败");
		}
		return resultBean;
	}
	
	
	public ResultBean updateBatch(List<EquipReqularCheckItem> list) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(list == null || list.size()<1) {
			throw new RuntimeException("数据为空");
		}
		ArrayList<EquipReqularCheckItemEntity> arrayList = new ArrayList<EquipReqularCheckItemEntity>();
		for(EquipReqularCheckItem item :list) {
			//校验数据
			ResultBean resultBean2 = checkData(item);
			if(!resultBean2.success) {
				return resultBean2;
			}
			EquipReqularCheckItemEntity innerData = item.getInnerData();
			baseEntityDataUtil.updateBaseEntityData(innerData);
			arrayList.add(innerData);
		}
		boolean batchById = updateBatchById(arrayList);
		if(!batchById) {
			throw new RuntimeException("修改失败");
		}
		return resultBean;
	}
	
	public ResultBean delete(EquipReqularCheckItem obj) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipReqularCheckItemEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");
		}
		EquipReqularCheckItemEntity selectById = EquipReqularCheckItemDao.selectById(innerData.getId());
		if(selectById == null) {
			throw new RuntimeException("删除数据不存在");
		}
		baseEntityDataUtil.deleteBaseEntityData(selectById);
		int delete = EquipReqularCheckItemDao.updateById(selectById);
		if(delete<1) {
			throw new RuntimeException("删除失败");
		}
		return resultBean;
	}
	
	public ResultBean deleteBatch(List<EquipReqularCheckItem> list) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(list == null || list.size()<1) {
			throw new RuntimeException("数据为空");
		}
		ArrayList<Long> listIds = new ArrayList<Long>();
		for(EquipReqularCheckItem item:list) {
			EquipReqularCheckItemEntity innerData = item.getInnerData();
			if(innerData == null ) {
				throw new RuntimeException("数据为空");
			}
			listIds.add(innerData.getId());
		}
		List<EquipReqularCheckItemEntity> selectBatchIdsList = EquipReqularCheckItemDao.selectBatchIds(listIds);
		if(selectBatchIdsList == null || selectBatchIdsList.size()<1) {
			throw new RuntimeException("数据为空");
		}
		for(EquipReqularCheckItemEntity item :selectBatchIdsList) {
			baseEntityDataUtil.deleteBaseEntityData(item);
		}
		boolean batchById = updateBatchById(selectBatchIdsList);
		if(!batchById) {
			throw new RuntimeException("删除失败");
		}
		return resultBean;
	}
	
	public EquipReqularCheckItemBean selectByCondition(EquipReqularCheckItem obj) {
    	QueryWrapper<EquipReqularCheckItemEntity> queryWrapper = new QueryWrapper<EquipReqularCheckItemEntity>();
    	queryWrapper.eq("row_status",1);
    	if(obj != null && obj.getInnerData() != null) {
    	}
        Page<EquipReqularCheckItemEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
        IPage<EquipReqularCheckItemEntity> iPage = EquipReqularCheckItemDao.selectPage(page, queryWrapper);
        List<EquipReqularCheckItemEntity> list = iPage.getRecords();
        if(list != null && list.size()>0) {
        	long pages = iPage.getPages();
        	long total = iPage.getTotal();
        	ArrayList<EquipReqularCheckItem> arrayList = new ArrayList<EquipReqularCheckItem>();
        	for(EquipReqularCheckItemEntity item :list) {
        	EquipReqularCheckItem EquipReqularCheckItem = new EquipReqularCheckItem();
        	EquipReqularCheckItem.setInnerData(item);
        	arrayList.add(EquipReqularCheckItem);
        	}
        	EquipReqularCheckItemBean EquipReqularCheckItemBean = new EquipReqularCheckItemBean();
        	EquipReqularCheckItemBean.setList(arrayList);
        	EquipReqularCheckItemBean.setPages((int)pages);
        	EquipReqularCheckItemBean.setTotal((int)total);
        	return EquipReqularCheckItemBean;
        }
		return null;
		
	}
	
	public EquipReqularCheckItem getById(long id) {
		// TODO Auto-generated method stub
		EquipReqularCheckItemEntity appendEntity = EquipReqularCheckItemDao.selectById(id);
		if(appendEntity != null) {
			EquipReqularCheckItem EquipReqularCheckItem = new EquipReqularCheckItem();
			EquipReqularCheckItem.setInnerData(appendEntity);
			return EquipReqularCheckItem;
		}
		return null;
	}
	
	public List<EquipReqularCheckItem> getByEquipReqularCheckId(long id) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("regular_check_id", id);
		map.put("row_status", 1);
		List<EquipReqularCheckItemEntity> selectByMap = EquipReqularCheckItemDao.selectByMap(map);
		if(selectByMap != null && selectByMap.size()>0) {
			ArrayList<EquipReqularCheckItem> arrayList = new ArrayList<EquipReqularCheckItem>();
			for(EquipReqularCheckItemEntity item:selectByMap) {
				EquipReqularCheckItem EquipReqularCheckItem = new EquipReqularCheckItem();
				EquipReqularCheckItem.setInnerData(item);
				arrayList.add(EquipReqularCheckItem);
			}
			return arrayList;
				
		}
		return null;
	}
	
	public ResultBean checkData(EquipReqularCheckItem obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipReqularCheckItemEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");
		}
		if(innerData.getRegularCheckId() == 0) {
			throw new RuntimeException("无法获取父级主键id");
		}
		if(innerData.getItemName() == null ||"".equals(innerData.getItemName())) {
			throw new RuntimeException("基准项目为空");
		}
		if(innerData.getItemStandard() == null || "".equals(innerData.getItemStandard())) {
			throw new RuntimeException("判定标准为空");
		}
		if(innerData.getItemMethod() == null || "".equals(innerData.getItemMethod())) {
			throw new RuntimeException("操作要领为空");
		}
		return resultBean;
		
	}
}
